﻿using AspNetTraining.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspNetTraining
{
    public partial class PermissionDetail : System.Web.UI.Page
    {
        Permission permiss = new Permission();
        PermissionManager permissManager = new PermissionManager();
        public UserAccount SessionLogin
        {
            get
            {
                return (UserAccount)Session["LoggedUser"];
            }
            set
            {
                Session["LoggedUser"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!IsPostBack)
            {
                /// set value for DropDowlist Groupmanager
                GroupUserManager groupuser = new GroupUserManager();
                List<GroupUser> Lgroupuser = groupuser.GetGroupUser();
                foreach (GroupUser g in Lgroupuser)
                {
                    DDLGroupUser.Items.Add(new ListItem(g.GroupName, g.GroupUserID.ToString()));
                }
                /// set value for DropDowlist FuncManager
                FunctionalityManager FuncManager = new FunctionalityManager();
                List<Functionality> funclist = FuncManager.GetListFunction();
                foreach (Functionality f in funclist)
                {
                    DDLFunction.Items.Add(new ListItem(f.FunctionName, f.FunctionID.ToString()));
                }
                
            }
            if (Request["id"] != null)
            {
                /// keep Permission of Permissions.aspx tranfer to.
                try { permiss = permissManager.GetPermissionById(new Guid(Request["id"])); }
                catch (Exception ee)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "console.log(" + ee.ToString() + ")", true);
                }
                if (!IsPostBack)
                    SetValue(permiss);
            }
           
        }
        private void SetValue(Permission pers)
        {
            lblId.Text = pers.PermissionID.ToString() ;
            DDLFunction.SelectedValue = pers.FunctionID;
            DDLGroupUser.SelectedValue = pers.GroupUserID;
            chkActive.Checked = (bool)pers.Active;
            chkAllowAdd.Checked = (bool)pers.AllowAdd;
            chkAllowDelete.Checked = (bool)pers.AllowDelete;
            chkAllowEdit.Checked = (bool)pers.AllowEdit;
            chkAllowExport.Checked = (bool)pers.AllowExport;
            chkAllowImport.Checked = (bool)pers.AllowImport;
            chkAllowPrint.Checked = (bool)pers.AllowPrint;
            chkAllowRead.Checked = (bool)pers.AllowRead;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Permission permission = new Permission();
            permission.Active = chkActive.Checked;
            permission.AllowAdd = chkAllowAdd.Checked;
            permission.AllowDelete = chkAllowDelete.Checked;
            permission.AllowEdit = chkAllowEdit.Checked;
            permission.AllowExport = chkAllowExport.Checked;
            permission.AllowImport = chkAllowImport.Checked;
            permission.AllowPrint = chkAllowPrint.Checked;
            permission.AllowRead = chkAllowRead.Checked;
            permission.FunctionID = DDLFunction.SelectedValue;
            permission.GroupUserID = DDLGroupUser.SelectedValue;
            //edit
            if (Request["id"] != null)
            {
                permission.PermissionID = permiss.PermissionID;
                permission.ModifiedBy = SessionLogin.UserAccountID;
                permission.ModifiedOn = DateTime.Now;
                permission.CreatedBy = permiss.CreatedBy;
                permission.CreatedOn = permiss.CreatedOn;
                if(permissManager.UpdatePermission(permission))
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "alert(\"Edited Successful\")", true);
                }
            }// insert
            else {
                permission.PermissionID = Guid.NewGuid();
                permission.CreatedBy = SessionLogin.UserAccountID.ToString() ;
                permission.CreatedOn = DateTime.Now;
                if (permissManager.InsertPermission(permission))
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "alert(\"Inserted Successful\")", true);
                }
            }
            Response.Redirect("~/Permissions.aspx");
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Permissions.aspx");
        }
    }
}